package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Friends;
import com.cloudwise.gproject.entity.User;
import com.cloudwise.gproject.service.FriendsService;
import com.cloudwise.gproject.service.UserService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * (Friends)表控制层
 *
 * @author makejava
 * @since 2021-05-06 22:18:07
 */
@Controller
@RequestMapping("friends")
public class FriendsController {
    /**
     * 服务对象
     */
    @Resource
    private FriendsService friendsService;
    @Resource
    private UserService userService;

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.friendsService.queryById(id));
    }

    /**
     * 跳转部门分页列表页面
     * @return
     */
    @RequestMapping("toFriends")
    public String toFriends() {
        return "/back/friends";
    }

    /**
     * 根据当前userInfo中存储的ID查询所有好友
     * @param page
     * @param limit
     * @param request
     * @return Result
     */
    @ResponseBody
    @RequestMapping("queryAllFriends")
    public Result queryAll(Integer page, Integer limit, HttpServletRequest request) {
        HttpSession session = request.getSession();
        User userInfo = (User)session.getAttribute("userInfo");
        int userId = userInfo.getUserId();
        PageInfo pageInfo = userService.queryAllFriends(page, limit, userId);
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 新增数据
     * @param friends 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Friends friends) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.friendsService.insert(friends));
    }

    /**
     * 修改数据
     * @param friends 实例对象
     * @return 实例对象
     */
    @ResponseBody
    @RequestMapping("update")
    public Result update(Friends friends) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.friendsService.update(friends));
    }

    /**
     * 通过主键删除数据
     * @param friendsId 主键
     * @return 是否成功
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer friendsId) {
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0,
                this.friendsService.deleteById(friendsId));
    }
}
